# -*- coding: utf-8 -*-
from selenium.webdriver.common.by import By
from base_page.basepage import BasePage

import allure
import time
from config import log
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


# 对象层

class EnrollPage(BasePage):
    def __init__(self, type="app"):
        # 继承基类的属性
        # super().__init__()
        super().__init__(type)
        self.driver.switch_to.context(self.driver.contexts[-1])
        self.user_enroll = (By.XPATH, '//a[text()="用户注册"]')  # 用户注册
        self.el_phone = (By.CSS_SELECTOR, "#phone")  # 手机号
        self.el_email = (By.CSS_SELECTOR, "#email")  # 邮箱
        self.el_password = (By.CSS_SELECTOR, "#password")  # 密码
        self.el_smscode = (By.ID, "#smsCode")
        self.el_code = (By.XPATH, '//*[text()="发 送"]')  # 发送
        self.el_deal = (By.XPATH, '//input[@class="am-radio-input"]')  # 协议1
        # self.el_deal2 = (By.XPATH, '//input[@class="am-radio-input"]', 1)  # 协议2
        # self.el_deal3 = (By.XPATH, '//input[@class="am-radio-input"]', 2)  # 协议3
        self.Wait = WebDriverWait

    # 用户 注册按钮
    @allure.step(title='用户注册按钮')  # step 报告标记测试步骤
    def find_enroll(self):
        # 使用官方类方法
        # return self.driver.find_element("id", 'username')

        # 定义方法进行解包（basepage）
        return self.find_element(self.user_enroll)

        # 定义方法根据判断调用不同方法（basepage）
        # return self.find_element_if("id", "username")

    #  手机号输入框
    @allure.step(title='定位手机号')
    def find_phone(self):
        # return self.driver.find_element("id", 'password')
        return self.find_element(self.el_phone)

    #   邮箱输入框
    @allure.step(title='邮箱输入框')
    def find_email(self):
        return self.find_element(self.el_email)

    def find_pwd(self):
        return self.find_element(self.el_password)

    #  获取smscode输入框
    @allure.step(title='获取send按钮')
    def find_smscode(self):
        return self.find_element(self.el_smscode)

    #  获取send按钮
    @allure.step(title='获取send按钮')
    def find_code(self):
        return self.find_element(self.el_code)

    # 三个协议
    def find_deal(self):
        return self.Wait(self.driver, 10, 0.2).until(
            lambda x: self.find_elements(self.el_deal))


# 操作层


class EnrollHandle(object):
    def __init__(self):
        self.page = EnrollPage()

    def click_enroll(self):
        self.page.find_enroll().click()

    def input_phone(self, phone):
        self.page.find_phone().send_keys(phone)

    def input_email(self, email):
        self.page.find_email().send_keys(email)

    def input_pwd(self, pwd):
        self.page.find_pwd().send_keys(pwd)

    def click_smscode(self):
        self.page.find_smscode().click()

    def click_getcode(self):
        self.page.find_code().click()

    def click_el(self):
        self.page.find_deal()[0].click()


# 逻辑层
class EnrollProxy(object):
    def __init__(self):
        self.handle = EnrollHandle()

    # 使用用户名和密码登录
    def get_user(self, phone, email, pwd):
        log.info(f"正在打开用户注册页面")
        self.handle.click_enroll()
        time.sleep(2)
        log.info("正在输入手机号")
        self.handle.input_phone(phone)
        time.sleep(1.5)
        log.info("输入邮箱")
        self.handle.input_email(email)
        time.sleep(1.5)
        log.info("正在输入密码")
        self.handle.input_pwd(pwd)
        time.sleep(1.5)
        log.info("点击验证码输入框")
        self.handle.click_smscode()
        time.sleep(1.5)

        log.info("正在获取验证码")
        self.handle.click_getcode()
